github.com/sandwich-go/boost@v1.3.29/xos/gorun .go (about) 1 package xos 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "sync" 8 9 "github.com/sandwich-go/boost/xpanic" 10 ) 11 12 const PathSeparator = string(os.PathSeparator) 13 14 var goRunOnce sync.Once 15 var isGoRun bool 16 17 // IsGoRun returns true if the binary is run from a go run command. 18 func IsGoRun() bool { 19 goRunOnce.Do( 20 func() { 21 ex, _ := os.Executable() 22 exPath := filepath.Dir(ex) 23 isGoRun = strings.Contains(exPath, "go-build") || strings.Contains(ex, "go_build_") || strings.Contains(exPath, "/private/var/folders/") 24 }, 25 ) 26 return isGoRun 27 } 28 29 // MustGetBinaryFilePath returns binary path if the binary is run from a go run command. 30 func MustGetBinaryFilePath() (ret string) { 31 ex, err := os.Executable() 32 xpanic.WhenErrorAsFmtFirst(err, "Executable got error:%w") 33 realPath, err0 := filepath.EvalSymlinks(ex) 34 xpanic.WhenErrorAsFmtFirst(err0, "EvalSymlinks got error:%w") 35 return realPath 36 }